/*
The MIT License
Copyright (c) 2019 Lehrstuhl Informatik 11 - RWTH Aachen University
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE

This file is part of embeddedRTPS.

Author: i11 - Embedded Software, RWTH Aachen University
*/

#ifndef RTPS_STATEFULREADER_H
#define RTPS_STATEFULREADER_H

#include "lwip/sys.h"
#include "rtps/communication/UdpDriver.h"
#include "rtps/config.h"
#include "rtps/entities/Reader.h"
#include "rtps/entities/WriterProxy.h"
#include "rtps/storages/MemoryPool.h"
#include "rtps/messages/MessageTypes.h"

struct rtps_StatefulReader {
	struct rtps_Reader m_reader;
	rtps_PacketInfo m_packetInfo; // TODO intended for reuse but buffer not used as such
	rtps_UdpDriver *m_transport;

	ddsReaderCallback_fp m_callback;
	void *m_callee;
	sys_mutex_t m_mutex;
};
typedef struct rtps_StatefulReader StatefulReader;

void rtps_StatefulReader_init(struct rtps_StatefulReader *sfReader,
			      const TopicData *attributes, rtps_UdpDriver *driver);
void rtps_StatefulReader_newChange(struct rtps_StatefulReader *sfReader,
				   const ReaderCacheChange *cacheChange);
void rtps_StatefulReader_registerCallback(struct rtps_StatefulReader *sfReader,
		ddsReaderCallback_fp cb, void *callee);
bool rtps_StatefulReader_addNewMatchedWriter(struct rtps_StatefulReader *sfReader,
		const WriterProxy *newProxy);
void rtps_StatefulReader_removeWriter(struct rtps_StatefulReader *sfReader,
				      const Guid_t *guid);
void rtps_StatefulReader_removeWriterOfParticipant(struct rtps_StatefulReader *sfReader,
		const GuidPrefix_t *guidPrefix);
bool rtps_StatefulReader_onNewHeartbeat(struct rtps_StatefulReader *sfReader,
		struct SubmessageHeartbeat *msg, const GuidPrefix_t *sourceGuidPrefix);
void rtps_StatefulReader_Construct(struct rtps_StatefulReader *sfReader);
void rtps_StatefulReader_Destruct(struct rtps_StatefulReader *sfReader);
#endif // RTPS_STATEFULREADER_H
